package com.dagit.network;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import com.dagit.core.pool.CommunicationException;
import com.dagit.core.pool.ObjectPool;
import com.dagit.network.traffic.Message;
import com.dagit.network.traffic.RegisterInstanceMessage;

public class LocalClientSocketPool extends ObjectPool<Socket>{
	private Instance instance;
	
	public LocalClientSocketPool(Instance instance){
		super();
		this.instance = instance;
	}

	@Override
	protected Socket create() throws CommunicationException{
		try {
			SocketImpl socket = new SocketImpl(instance.getIp(), instance.getPort());
			socket.setLocalClientSocketPool(this);
			return socket;
		} catch (Exception e) {
			throw new CommunicationException(e);
		} 
	}

	@Override
	public boolean validate(Socket o) {
		return o.isConnected();
	}

	@Override
	public void expire(Socket o) throws CommunicationException {
		try {
			o.close();
		} catch (IOException e) {
			throw new CommunicationException(e);
		}		
	}

		
}
